//
//  RegisterVC.m
//  sensen_ios
//
//  Created by pro on 16/2/1.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "RegisterVC.h"
#import "WebViewController.h"


#import "UserOperationView.h"

@interface RegisterVC ()

@property (nonatomic, strong) UserOperationView *userView;
@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBarButtonItemWithImagename:nil]; // 左侧返回按钮
    
    [self _createViews]; // 创建视图
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 设置导航栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
}

#pragma mark - 创建视图
- (void)_createViews {
    
    [self _createUserView];
    [self _createButtons];
}

// 创建用户操作视图（登陆，注册等）
- (void)_createUserView {
        
    _userView = [[UserOperationView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, lineHeight * 4)
                                                  titles:@[@"手机号", @"请输入验证码", @"密码", @"请输入邀请码（选填）"]
                                                    type:ZKUserViewRegister];
    [self.scrollView addSubview:_userView];
}
- (void)_createButtons {
    CGFloat x = 20;
    CGFloat y = _userView.bottom + 20;
    CGFloat width = SCREEN_WIDTH - 2 * x;
    CGFloat height = 40;
    
    // 登录按钮
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(x, y, width, height);
    registerButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    registerButton.layer.cornerRadius = 2;
    [self.scrollView addSubview:registerButton];
    registerButton.tag = 99;
    registerButton.enabled = NO;
    
    // 注册代表同意 label
    y = registerButton.bottom + 5;
    height = 20;
    NSString *agreeStr = @"注册代表同意";
    UILabel *agreeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    agreeLabel.font = FONT(14);
    agreeLabel.text = agreeStr;
    agreeLabel.textColor = [UIColor darkGrayColor];
    agreeLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:agreeLabel];
    
    //《用户服务协议》label
    NSString *protocolStr = @"《用户服务协议》";
    UILabel *protocolLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    protocolLabel.userInteractionEnabled = YES;
    protocolLabel.font = FONT(14);
    protocolLabel.text = protocolStr;
    protocolLabel.textColor = SS_NAVI_ORANGE_COLOR;
    protocolLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:protocolLabel];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [protocolLabel addGestureRecognizer:tap];
    

    // 设置两个label的位置
    CGFloat agreeLabelWidth = [agreeStr getSizeOfString:14].width;
    agreeLabel.width = agreeLabelWidth; // 设置宽度
    CGFloat protocolLabelWidth = [protocolStr getSizeOfString:14].width;
    protocolLabel.width = protocolLabelWidth; // 设置宽度
    // 设置位置
    agreeLabel.left = (SCREEN_WIDTH - agreeLabelWidth - protocolLabelWidth) / 2;
    protocolLabel.left = agreeLabel.right;


}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    WebViewController *vc = [[WebViewController alloc] initWithUrlString:@"http://sunsun.itboye.com/Public/html/copyright.html"];
    vc.titleString = @"用户服务协议";
    [self.navigationController pushViewController:vc animated:YES];
    
}



#pragma mark - 点击事件
- (void)registerAction:(UIButton *)button {
    [_userView getRegistrtInfo:^(NSDictionary *infoDic) {

        // 开始注册
        // 构造userModel
        UserModel *user = [[UserModel alloc] init];
        user.username = infoDic[@"mobile"];
        user.password = infoDic[@"password"];
        
        // 获取userModel的字典形式
        NSMutableDictionary *userDic = [NSMutableDictionary dictionaryWithCapacity:1];
        [userDic setObject:@"3" forKey:@"reg_type"]; // 注册类型，1：用户名，3:手机号注册
        [userDic setObject:@"0" forKey:@"from"]; // 注册来源(0、自主注册,1、qq,2微信,3、新浪微博,4、百度,99其他)
        [userDic setObject:infoDic[@"code"] forKey:@"code"];//invite_id
        [userDic setObject:infoDic[@"invite_id"] forKey:@"invite_id"];
        [userDic setObject:infoDic[@"mobile"] forKey:@"username"];
        [userDic setObject:infoDic[@"password"] forKey:@"password"];
       
        
        [BoyeHttpClient post:@"BY_User_register" apiVerson:100 params:userDic success:^(AFHTTPRequestOperation *operation, id result) {
            NSLog(@"%@", result);
            
            
            
            [SVProgressHUD showSuccessWithStatus:@"注册成功" withDuration:1];
            
            
            
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:1];
            NSLog(@"-------------%@", error);
        }];
    }];

}

#pragma mark - 其他
// 输入框内容改变通知事件
- (void)allTextFieldEndEditing {
    UIButton *reigsterButton = [self.view viewWithTag:99];
    if ([_userView isAllTextFieldEdited]) {
        reigsterButton.backgroundColor = SS_NAVI_ORANGE_COLOR;
        reigsterButton.enabled = YES;
    } else {
        reigsterButton.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
        reigsterButton.enabled = NO;
    }
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end
